package com.enjoyor.soa.traffic.restt.tpcms.dubbo.invoke;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Component;

import com.alibaba.dubbo.config.annotation.Reference;
import com.enjoyor.soa.traffic.api.ubms.dto.PublicDicDto;
import com.enjoyor.soa.traffic.api.ubms.dto.VehicleDicDto;
import com.enjoyor.soa.traffic.api.ubms.service.dubbo.DubboPublicDicService;
import com.enjoyor.soa.traffic.api.ubms.service.dubbo.DubboVehicleDicService;
import com.enjoyor.soa.traffic.util.enums.EnumAppCode;
import com.enjoyor.soa.traffic.util.pojo.ResultPojo;
import com.enjoyor.soa.traffic.util.pojo.TreeNode;

@Component
public class UbmsDicServiceInvoke {

    @Reference(timeout = 10000)
    private DubboPublicDicService dubboPublicDicService;

	@Reference(timeout = 10000)
	private DubboVehicleDicService dubboVehicleDicService;
    
	/**
     * 车辆类型字典List
     */
    public static List<VehicleDicDto> vehicleDicListCommon = new ArrayList<VehicleDicDto>();
	
    @SuppressWarnings("unchecked")
    public List<PublicDicDto> getPublicDic(String parentCode) {
        List<PublicDicDto> publicDicList = new ArrayList<PublicDicDto>();
        try {
            ResultPojo resultDic = dubboPublicDicService.getPublicDicByParentCode(parentCode);
            if (resultDic.getAppCode().equals(String.valueOf(EnumAppCode.SUCCESS.getCode()))) {
                publicDicList = (List<PublicDicDto>) resultDic.getResultList();
            }
        } catch (Exception ex) {
            throw ex;
        }
        return publicDicList;
    }

    public ResultPojo getPublicDicR(String parentCode) {
        ResultPojo resultDic = dubboPublicDicService.getPublicDicByParentCode(parentCode);
        return resultDic;
    }
    
    /**
	 * 获取车辆类型下拉树
	 * @return
	 */
	public List<TreeNode> getVehtypeTree(){
		//车辆类型字典缓存是否获取
		Boolean getVehicleDicStatic = false;
		if (vehicleDicListCommon == null || vehicleDicListCommon.size() <= 0) {
			getVehicleDicStatic = true;
		}
		List<TreeNode> vehtypeTreeList = new ArrayList<TreeNode>();
		try {
			ResultPojo parentPojo = dubboVehicleDicService.getVehicleDicByParentCode("VEHTYPE");
			if(parentPojo.getAppCode().compareTo("0") == 0 && parentPojo.getResultList() != null) {
				List<VehicleDicDto> parentList = (List<VehicleDicDto>) parentPojo.getResultList();
				for (VehicleDicDto parentDto : parentList) {
					String dicCode = parentDto.getDicCode();
					if(!"VEHTYPE01".equals(dicCode) && !"VEHTYPE10".equals(dicCode)) {
						TreeNode vehtypeTree1 = new TreeNode();
						vehtypeTree1.setId(dicCode);
						vehtypeTree1.setText(parentDto.getDicName());
						vehtypeTree1.setState("close");
						vehtypeTree1.setChecked(false);
						vehtypeTree1.setIcon(null);
						List<TreeNode> treeChildren1 = new ArrayList<TreeNode>();
						ResultPojo childrenPojo = dubboVehicleDicService.getVehicleDicByParentCode(dicCode);
						if(childrenPojo.getAppCode().compareTo("0") == 0 && childrenPojo.getResultList() != null) {
							List<VehicleDicDto> childrenList = (List<VehicleDicDto>) childrenPojo.getResultList();
							for (VehicleDicDto childrenDto : childrenList) {
								TreeNode vehtypeTree2 = new TreeNode();
								vehtypeTree2.setId(childrenDto.getDicCode());
								vehtypeTree2.setText(childrenDto.getDicName());
								vehtypeTree2.setState("close");
								vehtypeTree2.setChecked(false);
								vehtypeTree2.setIcon(null);
								treeChildren1.add(vehtypeTree2);
								
								if(getVehicleDicStatic) {
									vehicleDicListCommon.add(childrenDto);
								}
							}
						}
						
						vehtypeTree1.setChildren(treeChildren1);
						vehtypeTreeList.add(vehtypeTree1);
					}
				}
			}
		} catch (Exception ex) {
			throw ex;
		}
		return vehtypeTreeList;
	}
}
